package com.ljx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.common.ResponseResult;
import com.ljx.domain.entity.Article;
import com.ljx.domain.entity.Category;
import com.ljx.domain.vo.CategoryVo;
import com.ljx.domain.vo.PageVo;
import com.ljx.mapper.CategoryMapper;
import com.ljx.service.ArticleService;
import com.ljx.service.CategoryService;
import com.ljx.utils.BeanCopyUtils;
import com.ljx.utils.SystemConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-04-18 21:27:50
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    @Override
    public ResponseResult getCategoryList() {
        //查询已发布的文章
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articles = articleService.list(queryWrapper);
        //得到文章分类id值，需要去重
        Set<Long> ids = articles.stream().map(article ->
                article.getCategoryId()
        ).collect(Collectors.toSet());
        //根据id查出文章类型
        List<Category> categories = listByIds(ids);
        //过滤查询出状态是已经发布的
        List<Category> list = categories.stream().filter(category ->
                        SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //过滤为想要展示的数据
        List<CategoryVo> categoryVos = list.stream().map(num -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(num, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public List<CategoryVo> listAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return categoryVos;
    }

    @Override
    public ResponseResult listAll(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.hasText(name),Category::getName,name);
        lambdaQueryWrapper.eq(StringUtils.hasText(status),Category::getStatus,status);
        Page<Category> page = new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);
    }
}
